<template>
  <div style="padding:20px">
      <el-form
      :model="queryParams"
      ref="queryForm"
      size="small"
      :inline="true"
      v-show="showSearch"
    >
      <el-form-item prop="keyword" label="关键字:">
        <el-input
          v-model="queryParams.keyword"
          placeholder="请输入关键字"
          clearable
          style="width: 240px"
        />
      </el-form-item>

      <el-form-item prop="time" label="发布时间区间:">
        <el-date-picker
          v-model="time"
          type="datetimerange"
          @change="chooseTime()"
          value-format="yyyy-MM-dd HH:mm:ss"
          range-separator="-"
          start-placeholder="开始日期"
          end-placeholder="结束日期">
        </el-date-picker>
      </el-form-item>
      <el-form-item>
        <el-button
            type="primary"
            size="mini"
            @click="handleQuery"
            >搜索</el-button
          >
          <el-button size="mini" @click="resetQuery"
            >重置</el-button
          >
        </el-form-item>
    </el-form>
      <el-row :gutter="10" class="mb8">
        <el-button
            type="primary"
            plain
            icon="el-icon-plus"
            size="mini"
            @click="handleAdd"
            style="margin-bottom: 10px;"
            v-hasPermi="['society:articleUp:index']"
            >文章上传</el-button
          >
          <right-toolbar
            :showSearch.sync="showSearch"
            @queryTable="getArticleList"
          ></right-toolbar>
      </el-row>
    <div class="box_list">
      <el-table
        :data="articleList"
        style="width: 100%">
        <el-table-column
          prop="id"
          label="序号"
          align="center"
          width="180">
        </el-table-column>
        <el-table-column
          prop="title"
          align="center"
          label="标题"
          width="400">
          <template scope="scope">
            <div style="overflow: hidden;white-space: nowrap;width:400px;text-overflow:ellipsis;">{{ scope.row.title }}</div>
          </template>
        </el-table-column>
        <el-table-column
          prop="readingFrequency"
          align="center"
          label="阅读量"
          width="180">
        </el-table-column>
        <el-table-column
          prop="keyword"
          align="center"
          label="主要标签">
          <template scope="scope">
            <el-tag v-for="(item,index) in scope.row.keyword" v-if="index<3" :key="index" style="margin-right: 5px;">{{ item }}</el-tag>
          </template>
        </el-table-column>
        <el-table-column
          prop="releaseTime"
          align="center"
          label="发布时间"
          width="250">
        </el-table-column>
        <el-table-column
        label="操作"
        align="center"
        class-name="small-padding fixed-width"
        width="150"
      >
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            v-hasPermi="['society:articleEdit:edit']"
            @click="editArticle(scope.row)"
            >修改</el-button
          >
          <el-button
            size="mini"
            type="text"
            @click="del(scope.row.aid)"
            >删除</el-button
          >
        </template>
      </el-table-column>
      </el-table>

      <pagination
      v-show="total > 0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getArticleList"
    />
    </div>   
  </div>
</template>
<script>
import { list,delArticle } from '@/api/society';
export default {
  data(){
    return {
      //查询参数
      queryParams:{
        pageSize:10,
        pageNum:1,
        keyword:'',
        beginTime:'',
        endTime:'',
      },
      //时间
      time:'',
      //文章列表
      articleList:[],
      //总页数
      total:0,
      showSearch:true
    }
  },
  created(){
    this.getArticleList()
  },
  methods:{
    getArticleList(){
      let params = {
        ...this.queryParams
      }
      list(params).then(res =>{
        if(res.code == 200){
          this.articleList = res.rows
          this.total = res.total
          this.articleList.forEach((item,index)=>{
            this.$set(item, "id", index + 1);
            item.keyword = item.keyword.split(',')
          })
        }
      })
    },
    //时间分割
    chooseTime(){
      this.queryParams.beginTime = this.time[0]
      this.queryParams.endTime = this.time[1]
    },
    //编辑文章
    editArticle(row){
      this.$router.push({
          path: "/society/articleEdit",
          query: { key: row.aid },
        });
    },
    //文章删除
    del(aid){
      this.$confirm('确认删除该文章？', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(() => {
          delArticle(aid).then(res=>{
        if(res.code == 200){
          this.$message({
          showClose: true,
          message: '删除成功',
          type: 'success'
        });
        this.getArticleList()
        }
      })
        }).catch(() => {
          this.$message({
            type: 'info',
            message: '已取消删除'
          });          
        });
    },
    //文章上传
    handleAdd(){
      this.$router.push({
          path: "/society/articleUp",
        });
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.queryParams = {
        pageSize:10,
        pageNum:1,
        keyword:'',
        beginTime:'',
        endTime:'',
      },
      this.handleQuery();
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getArticleList();
    },
  }
}
</script>
<style scoped>
.box_list{
  background: white;
  min-height: 645px;
}
.title{
  font-family: Alibaba PuHuiTi;
  font-weight: bold;
  font-size: 16px;
  color: #000000;
}
</style>